package com.arong.swing.db.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.arong.swing.db.entity.TaskScope;
import com.arong.swing.exception.AppException;
import com.arong.swing.exception.SysException;
public class TaskScopeSDAO  extends BaseDAO {
	/*public TaskScopeSDAO(){
		this(DB_ANRONGTEC);
	}
	*/
	public TaskScopeSDAO(String dbFile){
		super(dbFile);
	}
	/*public TaskScopeSDAO(JDBCUtils jdbcUtils){
		super(jdbcUtils);
	}*/
	public String getCreateTableSql(){
		return "CREATE TABLE IF NOT EXISTS "+
				"TASK_SCOPE("+
				  "TASK_ID  VARCHAR2(32),"+
				  "TYPE  CHAR(1) NOT NULL,"+//--1组织机构id,2检查站id
				  "DEPT_ID NUMBER(12) NOT NULL,"+
				  "UP_DEPT_ID  NUMBER(12) NOT NULL"+
				")";
		
	}
	
	/**
	 * 插入
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  int insert(TaskScope entity) throws SysException,AppException {
		int i=0;
		try {
		 		i= jdbcUtils.insert(entity);
		} catch (SQLException e) {
			throw new SysException("add error", e);
		}
		return i;
	}

	/**
	 * 查询 集合
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public   List<TaskScope> queryList(TaskScope entity) throws SysException,AppException {
	    List<TaskScope> res=null;
	    try {
			res= jdbcUtils.queryList(getSql(entity), TaskScope.class,getParams(entity));
		} catch (SQLException e) {
			throw new SysException("queryList集合查询 error",e);
		}
		return   res;
	}
	public   List<TaskScope> queryListByTaskIds(String taskIds) throws SysException,AppException {
	    List<TaskScope> res=null;
	    try {
			res= jdbcUtils.queryList("SELECT a.TASK_ID,a.TYPE,a.DEPT_ID,a.UP_DEPT_ID from TASK_SCOPE a where TASK_ID in ("+taskIds+") ", TaskScope.class);
		} catch (SQLException e) {
			throw new SysException("queryList集合查询 error",e);
		}
		return   res;
	}
	/**
	 * 查询对象
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  TaskScope queryBean(TaskScope entity) throws SysException,AppException {
		TaskScope res=null;
	    try {
			res=  jdbcUtils.queryBean(getSql(entity), TaskScope.class,
				getParams(entity));
		} catch (SQLException e) {
			throw new SysException("queryBean对象查询 error",e);
		}
		return   res;
	}
	
	/**
	 * 获取查询条件
	 * @return
	 */
	private Object[] getParams(TaskScope entity) {
		List<Object> qryList = new ArrayList<Object>();
		if (entity.getTaskId() != null) {
			qryList.add(entity.getTaskId());
		}
		if (entity.getDeptId() != null) {
			qryList.add(entity.getDeptId());
		}
		
		return jdbcUtils.setQryList(qryList);
	}

	/**
	 * 获取sql
	 * @return
	 */
	private String getSql(TaskScope entity) {
		StringBuffer sql = new StringBuffer("SELECT a.TASK_ID,a.TYPE,a.DEPT_ID,a.UP_DEPT_ID from TASK_SCOPE a where 1=1");
		if (entity.getTaskId() != null) {
			sql.append(" and TASK_ID=?");
		}
		if (entity.getDeptId() != null) {
			sql.append(" and DEPT_ID=?");
		}
		
		return sql.toString();
	}
}

